package cn.isjinhao.se.backup;

import java.lang.reflect.*;
import java.util.*;

/**
 * @Author ISJINHAO
 * @Date 2021/3/10 21:22
 */
public class ClassMetaInfo {

    private Map<String, List<?>> parameterMap = new HashMap<>();

    private int[] sizes = new int[8];

    private int count = 0;

    private Set<String> conflictPropertySet = new HashSet<>();

    /**
     * tableName.propertyName(属性的全称), tableName
     */
    private Map<String, String> fullPropertyName = new HashMap<>();

    private Map<String, Class<?>> itemTypeMap = new HashMap<>();

    public ClassMetaInfo() {
    }


    public Map<String, List<?>> getParameterMap() {
        return parameterMap;
    }

    public int getCount() {
        return count;
    }

    public Map<String, String> getFullPropertyName() {
        return fullPropertyName;
    }

    public Map<String, Class<?>> getItemTypeMap() {
        return itemTypeMap;
    }

    public void addList(String tableName, List<?> list, Class<?> itemClass) {
        if (count >= 8) {
            throw new RuntimeException("一次select最多支持8个集合！");
        }
        sizes[count] = list.size();
        if (parameterMap.containsKey(tableName)) {
            throw new RuntimeException("单个select语句使用的集合名称不能重复！");
        }
        parameterMap.put(tableName, list);
        itemTypeMap.put(tableName, itemClass);
        extractPropertyName(tableName, itemClass);
        count++;
    }

    private void extractPropertyName(String tableName, Class<?> list) {
        Field[] declaredFields = list.getDeclaredFields();
        for (Field field : declaredFields) {
            String propertyName = field.getName();
            fullPropertyName.put(tableName + "." + propertyName, tableName);
            if(fullPropertyName.containsKey(propertyName)) {
                conflictPropertySet.add(propertyName);
            } else {
                fullPropertyName.put(propertyName, tableName);
            }
        }
    }

}
